//
//  ByRedPacketComeInMoreVC.m
//  mijifen_ios
//
//  Created by 朱凯 on 2017/11/13.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByRedPacketComeInMoreVC.h"

#import "ByCellRedPacketComeInListItem.h"

// view
#import "ByViewRedPacketReusableHeader.h"
#import "ByViewRedPacketComeInfomation.h"

@interface ByRedPacketComeInMoreVC () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collect;

@end

@implementation ByRedPacketComeInMoreVC

#pragma mark - view method
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"红包风暴";
    
    [self addLeftBackBarButtonItem];
    
    [self createCollectionView];
}

#pragma mark - 创建视图
- (void)createCollectionView {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 5;
    layout.minimumInteritemSpacing = 5;
    layout.itemSize = CGSizeMake((self.view.width - 5 * 2 - 8 * 2) / 3, 150);
    layout.sectionInset = UIEdgeInsetsMake(8, 8, 8, 8);
    
    self.collect = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    self.collect.backgroundColor = BY_BG_COLOR;
    self.collect.delegate = self;
    self.collect.dataSource = self;
    [self.view addSubview:self.collect];
    [self.collect mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(0, 0, 0, 0)).priorityMedium();
        if (@available(iOS 11.0, *)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-0);
            make.left.equalTo(self.view.mas_safeAreaLayoutGuideLeft);
            make.right.equalTo(self.view.mas_safeAreaLayoutGuideRight);
        }
    }];
    
    
    [self.collect registerClass:[ByCellRedPacketComeInListItem class] forCellWithReuseIdentifier:@"cell"];
    [self.collect registerClass:[ByViewRedPacketReusableHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 20;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ByCellRedPacketComeInListItem *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    return CGSizeMake(self.view.width, 60);
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    // 头视图
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        
        ByViewRedPacketReusableHeader *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        header.status = indexPath.section;
        return header;
    }
    
    return nil;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:false];
    
    ByViewRedPacketComeInfomation *view = [ByViewRedPacketComeInfomation new];
    [view show];
    
}

@end
